package com.java1234.view;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.java1234.dao.ILibrarianDao;
import com.java1234.dao.IUserDao;
import com.java1234.dao.impl.LibrarianImpl;
import com.java1234.dao.impl.UserImpl;
import com.java1234.model.Librarian;
import com.java1234.util.DbUtil;
import com.java1234.util.StringUtil;

public class LibrarianManageInterFrm extends JInternalFrame {
	private JTextField s_librarianNameTxt;
	private JTable librarianTable;
	private JTextField librarianAccountTxt;
	private JTextField librarianNameTxt;

	private DbUtil dbUtil = new DbUtil();
	private ILibrarianDao librarianDao = new LibrarianImpl();
	private IUserDao userDao = new UserImpl();
	private JTextField passwordTxt;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BorrowerManagerInterFrm frame = new BorrowerManagerInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LibrarianManageInterFrm() {
		setIconifiable(true);
		setClosable(true);
		setTitle("图书管理员信息");
		setBounds(100, 100, 928, 747);

		JLabel lblNewLabel = new JLabel("图书管理员姓名：");

		s_librarianNameTxt = new JTextField();
		s_librarianNameTxt.setColumns(10);

		JButton btnNewButton = new JButton("查询");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				librarianSearchActionPerformed(e);
			}
		});

		JScrollPane scrollPane = new JScrollPane();

		JScrollPane scrollPane_1 = new JScrollPane();

		JPanel panel = new JPanel();
		panel.setBorder(
				new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout.createSequentialGroup()
						.addGap(88).addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(82).addComponent(lblNewLabel)
										.addGap(31).addComponent(s_librarianNameTxt, GroupLayout.PREFERRED_SIZE, 219,
												GroupLayout.PREFERRED_SIZE)
										.addGap(26).addComponent(btnNewButton))
								.addGroup(groupLayout.createSequentialGroup()
										.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
												.addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE,
														GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
												.addComponent(scrollPane_1, Alignment.TRAILING,
														GroupLayout.DEFAULT_SIZE, 676, Short.MAX_VALUE))
										.addGap(122).addComponent(scrollPane, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addContainerGap(24, Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout.createParallelGroup(Alignment.LEADING).addGroup(groupLayout
						.createSequentialGroup().addGap(73).addGroup(
								groupLayout.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton)
										.addComponent(s_librarianNameTxt, GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(lblNewLabel))
						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createSequentialGroup().addGap(163).addComponent(scrollPane,
										GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
								.addGroup(groupLayout.createSequentialGroup().addGap(87).addComponent(scrollPane_1,
										GroupLayout.PREFERRED_SIZE, 210, GroupLayout.PREFERRED_SIZE)))
						.addGap(60).addComponent(panel, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(28, Short.MAX_VALUE)));

		JLabel lblNewLabel_1 = new JLabel("图书管理员账号：");

		JLabel lblNewLabel_2 = new JLabel("图书管理员姓名：");

		JButton btnNewButton_1 = new JButton("修改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				librarianUpdateActionEvent(e);
			}
		});

		JButton btnNewButton_2 = new JButton("删除");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				librarianDeleteActionPerformed(e);
			}
		});

		librarianAccountTxt = new JTextField();
		librarianAccountTxt.setColumns(10);

		librarianNameTxt = new JTextField();
		librarianNameTxt.setColumns(10);

		JLabel lblNewLabel_1_1 = new JLabel("图书管理员密码：");

		passwordTxt = new JTextField();
		passwordTxt.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addGap(29)
				.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panel.createSequentialGroup()
								.addComponent(lblNewLabel_1_1, GroupLayout.PREFERRED_SIZE, 120,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap())
						.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
								.addGroup(
										gl_panel.createSequentialGroup().addComponent(lblNewLabel_2).addContainerGap())
								.addGroup(gl_panel.createSequentialGroup()
										.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
												.addGroup(gl_panel.createSequentialGroup().addComponent(lblNewLabel_1)
														.addGap(28)
														.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
																.addComponent(librarianNameTxt,
																		GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
																.addComponent(librarianAccountTxt,
																		GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
																.addComponent(passwordTxt, GroupLayout.DEFAULT_SIZE,
																		299, Short.MAX_VALUE)))
												.addGroup(gl_panel.createSequentialGroup().addComponent(btnNewButton_1)
														.addGap(32).addComponent(btnNewButton_2)))
										.addGap(188))))));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(Alignment.LEADING).addGroup(gl_panel
				.createSequentialGroup().addContainerGap()
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1).addComponent(
						librarianAccountTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE))
				.addGap(29)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2)
						.addComponent(librarianNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
				.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_1_1).addComponent(
						passwordTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18).addGroup(gl_panel.createParallelGroup(Alignment.BASELINE).addComponent(btnNewButton_1)
						.addComponent(btnNewButton_2))
				.addGap(21)));
		panel.setLayout(gl_panel);

		librarianTable = new JTable();
		librarianTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				librarianTableMousePressed(e);
			}
		});
		librarianTable.setModel(
				new DefaultTableModel(new Object[][] {}, new String[] { "\u56FE\u4E66\u7BA1\u7406\u5458\u8D26\u53F7",
						"\u56FE\u4E66\u7BA1\u7406\u5458\u59D3\u540D", "\u5BC6\u7801" }) {
					boolean[] columnEditables = new boolean[] { false, false, false };

					public boolean isCellEditable(int row, int column) {
						return columnEditables[column];
					}
				});
		scrollPane_1.setViewportView(librarianTable);
		getContentPane().setLayout(groupLayout);
		// 设置文本域边框
		librarianAccountTxt.setBorder(new LineBorder(new java.awt.Color(127, 157, 185), 1, false));

		this.fillTable(new Librarian());
	}

	/**
	 * 初始化表格
	 * 
	 * @param bookType
	 */
	private void fillTable(Librarian librarian) {
		DefaultTableModel dtm = (DefaultTableModel) librarianTable.getModel();
		dtm.setRowCount(0);// 设置成0行
		Connection con = null;
		try {
			con = dbUtil.getCon();
			ResultSet rs = librarianDao.list(con, librarian);
			while (rs.next()) {
				Vector v = new Vector();
				v.add(rs.getString("librarianAccount"));
				v.add(rs.getString("librarianName"));
				v.add(rs.getString("password"));
				dtm.addRow(v);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 图书管理员搜索事件处理
	 * 
	 * @param e
	 */
	private void librarianSearchActionPerformed(ActionEvent evt) {
		
		String s_librarianName = this.s_librarianNameTxt.getText();
		Librarian librarian = new Librarian();
		librarian.setLibrarianName(s_librarianName);
		this.fillTable(librarian);
	}

	/**
	 * 表格行点击事件处理
	 * 
	 * @param e
	 */
	private void librarianTableMousePressed(MouseEvent evt) {
		int row = librarianTable.getSelectedRow();
		librarianAccountTxt.setText((String) librarianTable.getValueAt(row, 0));
		librarianNameTxt.setText((String) librarianTable.getValueAt(row, 1));
		passwordTxt.setText((String) librarianTable.getValueAt(row, 2));
	}

	/**
	 * 图书管理员信息修改处理
	 * 
	 * @param evt
	 */
	private void librarianUpdateActionEvent(ActionEvent evt) {
		String librarianAccount = librarianAccountTxt.getText();
		String librarianName = librarianNameTxt.getText();
		String password = passwordTxt.getText();
		if (StringUtil.isEmpty(librarianAccount)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录");
			return;
		}
		if (StringUtil.isEmpty(librarianName)) {
			JOptionPane.showMessageDialog(null, "借阅者姓名不能为空");
			return;
		}
		if (StringUtil.isEmpty(password)) {
			JOptionPane.showMessageDialog(null, "密码不能为空");
			return;
		}
		Librarian librarian = new Librarian(librarianAccount, librarianName, password);
		Connection con = null;
		try {
			con = dbUtil.getCon();
			int modifyNum = librarianDao.update(con, librarian);
			int n = userDao.updateLibrarian(con, librarian);
			if (modifyNum == 1 && n == 1) {
				JOptionPane.showMessageDialog(null, "修改成功");
				this.resetValue();
				this.fillTable(new Librarian());
			} else {
				JOptionPane.showMessageDialog(null, "修改失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "修改失败");
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 图书管理员删除事件处理
	 * 
	 * @param evt
	 */
	private void librarianDeleteActionPerformed(ActionEvent evt) {
		String librarianAccount = librarianAccountTxt.getText();
		if (StringUtil.isEmpty(librarianAccount)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的记录");
			return;
		}
		int n = JOptionPane.showConfirmDialog(null, "确定要删除该记录吗？");
		if (n == 0) {
			Connection con = null;
			try {
				con = dbUtil.getCon();

				int deleteNum = librarianDao.delete(con, librarianAccount);
				int d = userDao.deleteLibrarian(con, librarianAccount);
				if (deleteNum == 1 && d == 1) {
					JOptionPane.showMessageDialog(null, "删除成功");
					this.resetValue();
					this.fillTable(new Librarian());
				} else {
					JOptionPane.showMessageDialog(null, "删除失败");
				}
			} catch (Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "删除失败");
			} finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

	}

	/**
	 * 重置表单
	 */
	private void resetValue() {
		this.librarianAccountTxt.setText("");
		this.librarianNameTxt.setText("");
		this.passwordTxt.setText("");
	}
}
